package com.fingard.dsp.bank.directbank.cmbunion01;

import java.util.HashMap;
import java.util.Map;





import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IQRPay;
import com.fingard.dsp.bank.directbank.cmbunion01.util.BoingPay;
import com.fingard.dsp.bank.reqrespobj.qrpay.ReqGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.qrpay.RetAtsGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

public class CMBUnion01QRPay extends CMBUnifiedOrder implements IQRPay {

	@Override
	public RetAtsGenQRInfo genQRInfo(ReqGenQRInfo p_req) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public RetAtsUnifiedOrder submitQROrder(ReqUnifiedOrder p_req) {
		RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
		retAts.rdSeq = p_req.rdSeq;
		try{
			ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "");
			if (actItem != null) {
				Map<String, Object> order = new HashMap<String, Object>();
	            order.put("mch_app_id", p_req.ownAct);//商户appId
	            //if(p_req){}
	            String pay_interface_no = "";
				if ("NATIVE01".equalsIgnoreCase(p_req.reqHeader.commandCode)) {
					//微信
					pay_interface_no = "3004";
				}else if("NATIVE02".equalsIgnoreCase(p_req.reqHeader.commandCode)){
					//支付宝
					pay_interface_no = "3003";
				}else{
					retAts.respCode = "-1";
					retAts.respInfo = "未知的线路指令代码";
					return retAts;
				}
				order.put("pay_interface_no", pay_interface_no);// 支付通道编号 3004004
	            order.put("out_order_no", p_req.rdSeq);//商户订单号
	            order.put("pay_amount", changeCurToFen(p_req.amount));//支付金额
	            String notify_url = actItem.notifyURL;
				if (StringHelper.hasAnyChar(notify_url)) {
					order.put("back_url", notify_url);//后台通知地
				}
//	            order.put("expire", "2017-04-07 23:59:59");//过期时间
				if (StringHelper.hasAnyChar(p_req.productId)) {
					order.put("order_name", p_req.productId);
				}
	            
	            order.put("order_desc", p_req.description);

	            order.put("terminal_id", "");
	            boolean limit = getBankConfig().getValueAsBool("limitPay");
	            WriteBankUrlLogLn("招商聚合能否用信用卡支付："+limit);
	            if(!limit){
	            	order.put("limit_pay", "no_credit"); 
	            }
	            order.put("remark", p_req.attach);
	            JSONObject ext_user_info = new JSONObject();
				ext_user_info.put("name", p_req.oppActName);
				ext_user_info.put("mobile", p_req.cellPhone);
				String cert_type = getBankConfig().getMapValueByKey("certTypeMapping", p_req.certType);
				ext_user_info.put("cert_type", cert_type);
				ext_user_info.put("cert_no", p_req.certNumber);
				ext_user_info.put("min_age", "16");
				ext_user_info.put("fix_buyer", "T");
				ext_user_info.put("need_check_info", "T");
				order.put("ext_user_info", ext_user_info); 
	            WriteBankUrlLogLn("招商聚合地址："+bankFront.getUrl("unifiedPay"));
	            WriteBankUrlLogLn("招商聚合请求参数："+order);
	            String[] RSApath = actItem.ownCertFilePath.split(",");
	            setConfig(actItem.merchantId,bankFront.getUrl("unifiedPay"),RSApath[0],RSApath[1],actItem.ownKeyStorePath,actItem.oppCertFilePath,actItem.ownKeyPassword);
	            JSONObject ret = BoingPay.getCashier().createPayQRCode(order);
	            WriteBankLogStep3("招商聚合返回："+ret);
	            if(ret.getString("error_code") == null){
	            	retAts.respCode = "0";
					retAts.respInfo = "请求成功";
					retAts.qrInfo = ret.getString("qr_code");
					retAts.outTransID = ret.getString("order_no");
	            }else{
	            	retAts.respCode = "-1";
					retAts.respInfo = ret.getString("message")==null?"":ret.getString("message");
	            }
	            
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
				retAts.qrInfo = "";
			}
		}catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		return retAts;
	}

	@Override
	public RetAtsQueryOrder queryQRPay(ReqQueryOrder p_req) {
		return queryUnifiedOrder(p_req);
	}

}
